package com.woniu.sharepark.leasesystem.common.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.leasesystem.common.pojo.CarParkComment;
import com.woniu.sharepark.leasesystem.common.service.CarParkCommentReplyService;
import com.woniu.sharepark.leasesystem.common.service.CarParkCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName CarParkCommentController
 *@Description 对前端评论信息请求处理的controller
 *@Author litomng
 *@Date 2019/6/3 23:33
 *@Version 1.0
 **/
 @Controller
 @RequestMapping("/comment")
 public class CarParkCommentController {
    @Autowired
    private CarParkCommentService carParkCommentService;

    /**
     * 根据车位id分页查询所有评论
     * @title allComments
     * @param currentPage 当前页
     * @param  pageSize  每页条数
     * @return java.util.Map
     * @author litong
     * @date 2019/6/4 11:42
     **/
    @RequestMapping("/allComments")
    @ResponseBody
    public Map allComments(@RequestParam(value = "page" ,defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "rows",defaultValue = "5",required = false)Integer pageSize
                            ,Integer cId){
        List<CarParkComment> list=carParkCommentService.findCommentByPage(currentPage,pageSize,cId);
        PageInfo<CarParkComment> pageInfo=new PageInfo<>(list);
        Map result=new HashMap();
        result.put("rows",pageInfo.getList());
        result.put("total",pageInfo.getTotal());
        return result;
    }
    /**
    * 根据车位id查询所有对应评论及评论对应的回复
    * @title allCommentsAndReply
    * @param cId
    * @return com.woniu.sharepark.common.JsonResult
    * @author litong
    * @date 2019/6/13 10:26
    **/
    @RequestMapping("/allCommentsAndReply")
    @ResponseBody
    public JsonResult allCommentsAndReply(Integer cId){

     return carParkCommentService.findAllCommentsAndReply(cId);
    }
    /**
    * 添加评论
    * @title addComment
    * @param carParkComment
     * @param files
     * @param request
    * @return com.woniu.sharepark.common.JsonResult
    * @author litong
    * @date 2019/6/4 19:05
    **/
    @RequestMapping("/addComment")
    @ResponseBody
    public JsonResult addComment(CarParkComment carParkComment, MultipartFile files, HttpServletRequest request) throws IOException {
      return carParkCommentService.addComment(carParkComment,files,request);
    }
    /**
    * 通过评论id删除对应评论及其对应的所有回复（软删除）
    * @title deleteComment
    * @param cpcId 主键，评论id
    * @return com.woniu.sharepark.common.JsonResult
    * @author litong
    * @date 2019/6/4 19:24
    **/
    @RequestMapping("/deleteComment")
    @ResponseBody
    public JsonResult deleteComment(Integer cpcId){
        return carParkCommentService.deleteComment(cpcId);
    }
}
